package cn.hchaojie.javase.day01;

import org.junit.Test;

/**
 * 第一次运行的条件
 * 循环终止的条件
 * 循环次数
 * 
 * @author hchaojie
 *
 */
public class DemoLoop {
	@Test
	public void testWhile() {
		int count = 10;
		
		while (count > 0) {
			System.out.println(count);
			// count = count - 1;
			count--;
		}
	}

	@Test
	public void testWhile2() {
		int count = 10;
		
		while (count > 0) {
			count--;
			System.out.println(count);
		}
	}
	
	@Test
	public void testDoWhile() {
		int count = 10;
		
		do {
			count--;
			System.out.println(count);
		} while (count > 0);
	}
	
	@Test
	public void testDice() {
		int dice1 = 0;
		int dice2 = 0;
//		while (dice1 < 6 || dice2 < 6) {
		
//		while (!(dice1 == 6 && dice2 == 6)) {
		while ((dice1 != 6 || dice2 != 6)) {
			dice1 = (int) (Math.random() * 6 + 1);
			dice2 = (int) (Math.random() * 6 + 1);
			
			System.out.println(dice1 + " / " + dice2);
		}
	}
	
	@Test
	public void testSeries() {
		int i = 1;
		int sum = 0;
		while (i <= 10) {
			sum += i;
			i++;
		}
		
		System.out.println(sum);
	}
	
	@Test
	public void testSeries2() {
		int sum = 0;
		for (int i = 1; i <= 10; i++) {
			sum += i;
		}
		
		System.out.println(sum);
	}
	
	@Test
	public void testFactorial() {
		int fact = 1;
		int i = 1;
		for (; i <= 10; i++) {
		// for (int i = 1; i <= 10; i++) {
			fact *= i;
		}
		
		System.out.println(fact);
	}
	
	@Test
	public void testDoubleFactorial() {
		int n = 6;
		int fact = 1;
		int i = (n % 2 == 0) ? 2 : 1;
		for (; i <= n; i += 2) {
			fact *= i;
		}
		
		System.out.println(fact);
	}
	
	@Test
	public void testCountDown() {
		for (int i = 10; i > 0; i--) {
			System.out.println(i - 1);
		}
	}
	
	@Test
	public void print1() {
		for (int j = 1; j <=5; j++) {
			for (int i = 1; i <=5; i++) {
				System.out.print("* ");
			}

			System.out.println();
		}
	}
	
	@Test
	public void print2() {
		for (int j = 1; j <= 5; j++) {
			for (int i = 1; i <= j; i++) {
				System.out.print("* ");
			}

			System.out.println();
		}
	}
	
	@Test
	public void print3() {
		for (int j = -1; j <= 1; j++) {
			for (int i = 1; i >= -1; i--) {
				System.out.print(j + "," + i + "\t\t");
			}
			
			System.out.println();
		}
	}
}
